#include <GameEngine.hpp>

namespace EE
{
	GameEngine::GameEngine()
	{
		window = new sf::RenderWindow(sf::VideoMode(800, 600, 32), "Bluehollow");
	}

	GameEngine::~GameEngine()
	{
		delete window;
		window = NULL;
	}

	void GameEngine::Start()
	{
		Run();
	}

	void GameEngine::Stop()
	{
		isRunning = false;
	}

	void GameEngine::AddState(GameStateBase *state)
	{
		PushState(state);
	}

	void GameEngine::StopState()
	{
		PopState();
	}

	void GameEngine::PushState(GameStateBase *state)
	{
		if (!states.empty())
		{
			states.back()->Pause();
		}
		states.push_back(state);
		states.back()->Activate();
	}

	void GameEngine::PopState()
	{
		delete states.back();
		states.pop_back();
		states.back()->Activate();
	}

	void GameEngine::Run()
	{
		isRunning = true;

		while(isRunning && (!states.empty()))
		{
			sf::Event event;
			while (window->PollEvent(event))
			{
				states.back()->Process(event);
			}
			states.back()->Update();
			states.back()->UpdateTimed(window->GetFrameTime());
			window->Clear();
			states.back()->Render();
			window->Display();
		}
	}
}
